package com.yx.frame.jdbc;

import java.io.Reader;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

import com.yx.frame.util.Format;
import com.yx.frame.util.ZipUtils;

public final class SqlEntry {

	String sql;
	boolean isPagable = false;
	List<Reader> readerList = new ArrayList<Reader>();
	List<String[]> paramList = new ArrayList<String[]>();

	String[] paramType;

	public SqlEntry(String sql) {
		this.sql = sql;
	}

	public SqlEntry(String sql, boolean isPagable) {
		this.sql = sql;
		this.isPagable = isPagable;
	}

	public String getSql() {
		if (isPagable) {
			return sql + " LIMIT ?,? ";
		} else {
			return sql;
		}
	}

	public void copyParms(PreparedStatement stmt, int row) throws Exception {
		String[] values = paramList.get(row);
		for (int i = 0; i < values.length; i++) {
			if("String".equals(paramType[i])){
				stmt.setString(i + 1, values[i]);
			}else if("Int".equals(paramType[i])){
				if(values[i]==null)values[i]="0";
				stmt.setInt(i + 1, Integer.parseInt(values[i]));				
			}else if("Double".equals(paramType[i])){
				if(values[i]==null)values[i]="0";
				stmt.setDouble(i + 1, Double.parseDouble(values[i]));
			}else if("Long".equals(paramType[i])){
				if(values[i]==null)values[i]="0";
				stmt.setLong(i + 1, Long.parseLong(values[i]));
			}else if("Date".equals(paramType[i])){
				stmt.setDate(i + 1,	turnSqlDate(values[i]));
			}else if("Clob".equals(paramType[i])){
				if(values[i]==null)values[i]="";
				Reader reader = ZipUtils.getStringReader(values[i]);
				stmt.setCharacterStream(i + 1, reader, values[i].length());
				readerList.add(reader);
			}else if("ZipClob".equals(paramType[i])){
				if(values[i]==null)values[i]="";
				String zipValue = ZipUtils.gzip(values[i]);
				Reader zipReader = ZipUtils.getStringReader(zipValue);
				stmt.setCharacterStream(i + 1, zipReader, zipValue.length());
				readerList.add(zipReader);
			}else{
				stmt.setString(i + 1, values[i]);
			}
		}
	}
	
	static final java.sql.Date turnSqlDate(String date)throws Exception{		
		try{
			java.util.Date d=Format.formatDate(date);
			if(d==null)return null;		
			//System.out.println(new java.sql.Date(d.getTime()));
			return new java.sql.Date(d.getTime());
		}catch(Exception e){
			throw new Exception("日期格式出错："+date);
		}
	}

	public void closeClobReader() {
		int len = readerList.size();
		for (int i = 0; i < len; i++) {
			try {
				readerList.get(i).close();
			} catch (Exception e) {
			}
		}
		readerList.clear();
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public void addParms(String[] parm) {
		paramList.add(parm);
	}

	public String[] getParamType() {
		return paramType;
	}

	public void setParamType(String[] paramType) {
		this.paramType = paramType;
	}

	public int getParmsRowCount() {
		return paramList.size();
	}

	public Object[] getParms(int idx) {
		return paramList.get(idx);
	}

	public boolean isPagable() {
		return isPagable;
	}

	public void setPagable(boolean isPagable) {
		this.isPagable = isPagable;
	}
}
